import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20180509.DetectFaceRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

/**
 * 人脸检测代码示例
 */
public class DetectFaceSample extends BaseSample {

    public static void main(String[] args) throws Exception {
        //请替换成你自己的accessKeyId、accessKeySecret
        IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint(getEndPointName(), regionId, "Green", getDomain());

        IAcsClient client = new DefaultAcsClient(profile);

        DetectFaceRequest detectFaceRequest = new DetectFaceRequest();
        detectFaceRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
        detectFaceRequest.setMethod(MethodType.POST); // 指定请求方法
        detectFaceRequest.setEncoding("utf-8");
        detectFaceRequest.setProtocol(ProtocolType.HTTP);
        detectFaceRequest.setRegionId(regionId);
        detectFaceRequest.setHttpContentType(FormatType.JSON);

        JSONObject data = new JSONObject();
        data.put("url", "http://xxxx.xx/xx");
        detectFaceRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);

        /**
         * 请务必设置超时时间
         */
        detectFaceRequest.setConnectTimeout(3000);
        detectFaceRequest.setReadTimeout(10000);

        try {
            HttpResponse httpResponse = client.doAction(detectFaceRequest);
            if (httpResponse.isSuccess()) {
                JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                if (200 == scrResponse.getInteger("code")) {
                    JSONObject result = scrResponse.getJSONObject("data");
                    if (200 == result.getInteger("code")) {
                        System.out.println("task process success: " + result.toJSONString());
                        JSONArray faces = result.getJSONArray("faces");
                        System.out.println("faceCount:" + faces.size());
                    } else {
                        System.out.println("task process fail: " + result.toJSONString());
                    }
                } else {
                    System.out.println("task fail!!!" + scrResponse.toJSONString());
                }
            } else {
                System.out.println("response fail!!!" +
                        "\ncode:" + httpResponse.getStatus() +
                        "\nmsg:" + new String(httpResponse.getHttpContent(), "UTF-8"));
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
